/*
 * AddSaleLine.java
 *
 * Created on Feb 16, 2009, 10:59:26 AM
 */
package magis;

import edu.byu.intex2.data.Controller;
import edu.byu.intex2.data.Model;
import edu.byu.intex2.data.bo.PhysicalCar;
import edu.byu.intex2.data.bo.Sale;
import edu.byu.intex2.data.bo.SaleLine;
import edu.byu.intex2.data.dao.PhysicalCarDAO;
import edu.byu.intex2.data.dao.SaleLineDAO;

/**
 * Handles the creation of a new sale line
 * @author Brady
 */
public class AddSaleLine extends javax.swing.JFrame {

    private PhysicalCar car;
    private Sale sale;

    // jtableModel so that we can fire off changes when saleLine is created.
    SaleLineTableDataModel saleLinesTableModel = null;
    SaleItemsTableDataModel saleItemsTableModel = null;

    /** Creates new form AddSaleLine */
    public AddSaleLine(Sale sale, SaleLineTableDataModel sltdb, SaleItemsTableDataModel sitdb) {
        initComponents();
        Controller.getInstance().center(this);

        this.sale = sale;
        this.saleLinesTableModel = sltdb;
        this.saleItemsTableModel = sitdb;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saleCarGroup = new javax.swing.JPanel();
        makeDisplay_lbl = new javax.swing.JLabel();
        listPriceDisplay_lbl = new javax.swing.JLabel();
        colorDisplay_lbl = new javax.swing.JLabel();
        model_lbl = new javax.swing.JLabel();
        make_lbl = new javax.swing.JLabel();
        vin_txt = new javax.swing.JTextField();
        color_lbl = new javax.swing.JLabel();
        year_lbl = new javax.swing.JLabel();
        modelDisplay_lbl = new javax.swing.JLabel();
        yearDisplay_lbl = new javax.swing.JLabel();
        listPrice_lbl = new javax.swing.JLabel();
        salePrice_txt = new javax.swing.JTextField();
        findCar_btn = new javax.swing.JButton();
        listPrice_lbl1 = new javax.swing.JLabel();
        errorMessage_lbl = new javax.swing.JLabel();
        bottomMenu_pnl = new javax.swing.JPanel();
        cancel_btn = new javax.swing.JButton();
        saveCar_btn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        saleCarGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Sale Car"));

        makeDisplay_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        makeDisplay_lbl.setText("Not Supported Yet");

        listPriceDisplay_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        listPriceDisplay_lbl.setText("VIN Needed");

        colorDisplay_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        colorDisplay_lbl.setText("VIN Needed");

        model_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        model_lbl.setText("Model:");

        make_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        make_lbl.setText("Make:");

        vin_txt.setFont(vin_txt.getFont().deriveFont(vin_txt.getFont().getSize()-1f));
        vin_txt.setText("12345678901234567");

        color_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        color_lbl.setText("Color:");

        year_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        year_lbl.setText("Year:");

        modelDisplay_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        modelDisplay_lbl.setText("Not Supported Yet");

        yearDisplay_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        yearDisplay_lbl.setText("Not Supported Yet");

        listPrice_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        listPrice_lbl.setText("List Price:");

        salePrice_txt.setFont(salePrice_txt.getFont().deriveFont(salePrice_txt.getFont().getSize()-2f));
        salePrice_txt.setText("12000");

        findCar_btn.setFont(new java.awt.Font("Tahoma", 0, 8));
        findCar_btn.setText("Find Car");
        findCar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCar_btnActionPerformed(evt);
            }
        });

        listPrice_lbl1.setFont(new java.awt.Font("Tahoma", 0, 9));
        listPrice_lbl1.setText("Sale Price:");

        errorMessage_lbl.setFont(new java.awt.Font("Tahoma", 0, 9));
        errorMessage_lbl.setForeground(new java.awt.Color(153, 0, 0));
        errorMessage_lbl.setText(" ");

        javax.swing.GroupLayout saleCarGroupLayout = new javax.swing.GroupLayout(saleCarGroup);
        saleCarGroup.setLayout(saleCarGroupLayout);
        saleCarGroupLayout.setHorizontalGroup(
            saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saleCarGroupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(saleCarGroupLayout.createSequentialGroup()
                        .addComponent(vin_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(findCar_btn)
                        .addGap(67, 67, 67)
                        .addComponent(errorMessage_lbl, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                    .addGroup(saleCarGroupLayout.createSequentialGroup()
                        .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(listPrice_lbl1)
                            .addComponent(listPrice_lbl)
                            .addComponent(year_lbl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(yearDisplay_lbl)
                            .addComponent(listPriceDisplay_lbl)
                            .addComponent(salePrice_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(saleCarGroupLayout.createSequentialGroup()
                                .addComponent(make_lbl)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(makeDisplay_lbl))
                            .addGroup(saleCarGroupLayout.createSequentialGroup()
                                .addComponent(model_lbl)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(modelDisplay_lbl)))
                        .addGap(21, 21, 21)
                        .addComponent(color_lbl)
                        .addGap(18, 18, 18)
                        .addComponent(colorDisplay_lbl, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)))
                .addContainerGap())
        );
        saleCarGroupLayout.setVerticalGroup(
            saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saleCarGroupLayout.createSequentialGroup()
                .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vin_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(findCar_btn)
                        .addComponent(errorMessage_lbl)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(year_lbl)
                        .addComponent(yearDisplay_lbl))
                    .addGroup(saleCarGroupLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(listPriceDisplay_lbl)
                            .addComponent(listPrice_lbl)))
                    .addGroup(saleCarGroupLayout.createSequentialGroup()
                        .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(make_lbl)
                            .addComponent(makeDisplay_lbl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(model_lbl)
                            .addComponent(modelDisplay_lbl)))
                    .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(color_lbl)
                        .addComponent(colorDisplay_lbl)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(saleCarGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(listPrice_lbl1)
                    .addComponent(salePrice_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(saleCarGroup, java.awt.BorderLayout.CENTER);

        cancel_btn.setFont(new java.awt.Font("Tahoma", 0, 8));
        cancel_btn.setText("Cancel");
        cancel_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_btnActionPerformed(evt);
            }
        });
        bottomMenu_pnl.add(cancel_btn);

        saveCar_btn.setFont(new java.awt.Font("Tahoma", 0, 8));
        saveCar_btn.setText("Save Car");
        saveCar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveCar_btnActionPerformed(evt);
            }
        });
        bottomMenu_pnl.add(saveCar_btn);

        getContentPane().add(bottomMenu_pnl, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancel_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_btnActionPerformed
        this.dispose(); // Close Window
    }//GEN-LAST:event_cancel_btnActionPerformed

    private void saveCar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCar_btnActionPerformed
        // Validate SaleLine
        if (car == null) {
            errorMessage_lbl.setText("Valid Car Needed");
            return;
        } else if (salePrice_txt.getText().equals("")) {
            errorMessage_lbl.setText("Sale Price Needed");
            return;
        }
        
        // Add the New Sale Line
        try {
            SaleLine sl = SaleLineDAO.getInstance().create(sale.getId(), car.getVin());
            sl.setActualPrice(Double.valueOf(salePrice_txt.getText()));
            sl.setListPrice(car.getPrice());
            sale.addSaleLine(sl);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Tell jtableModel we have changed the table data
        saleLinesTableModel.fireTableDataChanged();
        saleItemsTableModel.fireTableDataChanged();
        this.dispose(); // Close Window
    }//GEN-LAST:event_saveCar_btnActionPerformed

    private void findCar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCar_btnActionPerformed
        try {
            // Find the Car by the VIN
            car = PhysicalCarDAO.getInstance().getByVIN(vin_txt.getText());
            updateTextFields(car);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_findCar_btnActionPerformed

    /** Update the Text Fields based on our newly found physical car */
    private void updateTextFields(PhysicalCar car) {
        //yearDisplay_lbl.setText(car);
        listPriceDisplay_lbl.setText(String.valueOf(car.getPrice()));
        colorDisplay_lbl.setText(car.getColor());
//        yearDisplay_lbl.setText(car.getYear());
//        makeDisplay_lbl.setText(car.getMake());
//        modelDisplay_lbl.setText(car.getModel());

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new AddSaleLine(Model.getInstance().getSale(), setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomMenu_pnl;
    private javax.swing.JButton cancel_btn;
    private javax.swing.JLabel colorDisplay_lbl;
    private javax.swing.JLabel color_lbl;
    private javax.swing.JLabel errorMessage_lbl;
    private javax.swing.JButton findCar_btn;
    private javax.swing.JLabel listPriceDisplay_lbl;
    private javax.swing.JLabel listPrice_lbl;
    private javax.swing.JLabel listPrice_lbl1;
    private javax.swing.JLabel makeDisplay_lbl;
    private javax.swing.JLabel make_lbl;
    private javax.swing.JLabel modelDisplay_lbl;
    private javax.swing.JLabel model_lbl;
    private javax.swing.JPanel saleCarGroup;
    private javax.swing.JTextField salePrice_txt;
    private javax.swing.JButton saveCar_btn;
    private javax.swing.JTextField vin_txt;
    private javax.swing.JLabel yearDisplay_lbl;
    private javax.swing.JLabel year_lbl;
    // End of variables declaration//GEN-END:variables
}
